﻿package newcommerce.lastws
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequestHeader;
	import flash.utils.Dictionary;
	
	public class AbstractClient extends EventDispatcher
	{
		protected var _requestQueue:Dictionary;
		protected var _requestId:Number = 0;
		
		public function AbstractClient() 
		{
			_requestQueue = new Dictionary();			
		}

		protected function runLoader(request:URLRequest, doComplete:Function, doError:Function, wrapper:RequestWrapper):Number
		{			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, doHttpStatus);
			loader.addEventListener(IOErrorEvent.IO_ERROR, doIOError);
			loader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR , doSecurityError);
			loader.addEventListener(Event.COMPLETE, doComplete);			
			loader.load(request);
			
			wrapper.requestId = _requestId++;
			wrapper.success = false;
			wrapper.errorCall = doError;
			wrapper.loader = loader;
			_requestQueue[loader] = wrapper;
			
			// trace("AbstractClient.runLoader(" + request.url + ")");
			
			return _requestId - 1;
		}
		
		protected function getLoaderIndex(loader:URLLoader):Number
		{
			return _requestQueue[loader].id;
		}
		
		protected function getWrapper(loader:URLLoader):RequestWrapper
		{
			return _requestQueue[loader];
		}
		
		protected function trash(loader:URLLoader):Object
		{
			var wrapper:Object = _requestQueue[loader];
			_requestQueue[loader] = null;
			
			return wrapper;
		}
		
		protected function doHttpStatus(evt:HTTPStatusEvent):void
		{
			//trace("AbstractClient.doHttpStatus:" + evt.status);
			if (evt.status == 201)
			{
			}
		}
		
		protected function doIOError(evt:IOErrorEvent):void
		{
			var wrapper:RequestWrapper = getWrapper(evt.target as URLLoader);
			wrapper.errorCall(evt, wrapper);
		}
		
		protected function doSecurityError(evt:SecurityErrorEvent):void
		{
			trace("AbstractClient.doSecurityError(" + evt.toString());
		}

		protected function handleProgress(evt:ProgressEvent):void
		{
			var percent:Number = Math.round(evt.bytesLoaded / evt.bytesTotal * 100);
			var idx:Number = getLoaderIndex(evt.target as URLLoader);
		}
		
		protected function handleIOError(evt:IOErrorEvent):void
		{
			trace("AbstractClient.ioError on loader:" + getLoaderIndex(evt.target as URLLoader));
		}
		
		protected function createWrapper(obj:Object = null):RequestWrapper
		{
			if (obj == null)
				obj = { };

			var wrapper:RequestWrapper = new RequestWrapper();
			for (var prop:String in obj)
			{
				wrapper[prop] = obj[prop];
			}
			
			//wrapper.successCall = successCall;
			//wrapper.errorCall = errorCall;

			return wrapper;
		}
	}
}